package elitech.vietnam.myfashion.view;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView2;
import com.huewu.pla.lib.MultiColumnListView;
import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.config.Const;
import elitech.vietnam.myfashion.dialog.UserInfoDialog;
import elitech.vietnam.myfashion.doinbackground.CallServiceTask;
import elitech.vietnam.myfashion.entites.Member;
import elitech.vietnam.myfashion.entites.Post;
import elitech.vietnam.myfashion.extend.AsyncTask;
import elitech.vietnam.myfashion.extend.GridStylerArrayAdapter;

public class MemberStyleView extends LinearLayout implements OnRefreshListener2<MultiColumnListView>, View.OnClickListener{

	private static final int NUM_ITEMS = 10;
	
	private MainActivity mContext;
	private PullToRefreshGridView2 mPullRefreshGridView;
	private MultiColumnListView mGridView;
	private GridStylerArrayAdapter mAdapter;
	private ImageView mAvatar;
	private TextView mUserName, mProfile, mTxtTitle;
	private ImageView mBtnNewPost;
	private Member mMember;
	private ArrayList<Post> mList = new ArrayList<Post>();

	private LinearLayout mMainMenu;
	private ImageButton mFloatMenu;
	
	// private ArrayList<Post> mListProduct = new ArrayList<Post>();

	public MemberStyleView(MainActivity context, Member member) {
		super(context);
		this.mContext = context;
		this.mMember = member;
		
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (inflater != null) {
			inflater.inflate(R.layout.mystyle, this);
		}
		this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

		mPullRefreshGridView = (PullToRefreshGridView2) this.findViewById(R.id.mystyler_gridview);
		mAvatar = (ImageView) this.findViewById(R.id.mystyle_imgAvatar);
		mUserName = (TextView) this.findViewById(R.id.mystyle_txtUsername);
		mProfile = (TextView) this.findViewById(R.id.mystyle_txtProfile);
		mBtnNewPost = (ImageView) this.findViewById(R.id.mystyle_imgNewPost);
		mTxtTitle = (TextView) findViewById(R.id.main_title);
		mMainMenu = (LinearLayout) findViewById(R.id.main_menu);
		mFloatMenu = (ImageButton) findViewById(R.id.mystyler_btnFloatMenu);
		mMainMenu.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mContext.callMenu();
			}
		});
		mFloatMenu.setVisibility(View.VISIBLE);
		mFloatMenu.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mContext.callMenu();
			}
		});
		mGridView = mPullRefreshGridView.getRefreshableView();

		mPullRefreshGridView.setMode(Mode.PULL_FROM_END);
		mPullRefreshGridView.setOnRefreshListener(this);
		mBtnNewPost.setVisibility(View.GONE);
		mAvatar.setOnClickListener(this);
		
		mAdapter = new GridStylerArrayAdapter(mContext, R.id.mystyler_gridview, 0, mList);
		mGridView.setAdapter(mAdapter);
		
		LoadMemberInfo();
		mTxtTitle.setText(R.string.styler);
		if (mList.size() == 0)
			GetMyStyle();

	}
	
	public void LoadMemberInfo() {
		UrlImageViewHelper.setUrlDrawable(mAvatar, Const.SERVER_IMAGE_THUMB_URL + mMember.Image, R.drawable.no_avatar);
		mUserName.setText((mMember.NickName.equals("")) ? mMember.Name : mMember.NickName);
		mProfile.setText(mMember.Profile);
	}
	
	public void onResume() {
		if (mPullRefreshGridView.isRefreshing())
			mPullRefreshGridView.onRefreshComplete();
	}

	private void GetMyStyle() {
		new CallServiceTask() {
			private ArrayList<Post> rs = new ArrayList<Post>();
			public void work() {
				try {
					int account = (mContext.mUser == null) ? -1 : mContext.mUser.Id;
					rs = service.getMemberStyle(account, mMember.Id, mList.size(), NUM_ITEMS);
				} 
				catch (NullPointerException e){}
				catch (IOException e) {}
				catch (XmlPullParserException e) {}
			};

			protected void onPostExecute(Void result) {
				if(rs!=null){
					mList.addAll(rs);
					mAdapter.notifyDataSetChanged();
				}
				mPullRefreshGridView.onRefreshComplete();
			};
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	@Override
	public void onPullDownToRefresh(
			PullToRefreshBase<MultiColumnListView> refreshView) {
	}

	@Override
	public void onPullUpToRefresh(
			PullToRefreshBase<MultiColumnListView> refreshView) {
		GetMyStyle();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.mystyle_imgAvatar:
			if (mMember != null)
				new UserInfoDialog(mContext, mMember).show();
			break;
		default:
			break;
		}
	}
}
